package com.qk.Interceptor;

import com.qk.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class TockenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("DemoInterceptor--------preHandle--------------------------");
        String path = request.getRequestURI();
        if (path.contains("login")) {
            log.info("登录操作: {}", path);
            return true;
        }
        String token = request.getHeader("token");
        if (token == null || token.isEmpty()) {
            log.info("token为空:{}", token);
            response.setStatus(401);
            return false;
        }
        //        解析 token
        try {
            JwtUtils.parseToken(token);
        } catch (Exception e) {
            log.info("token解析失败:{}", e.getMessage());
            response.setStatus(401);
            return false;
        }
        return true;
    }


}
